Javascript Testing

Frisby.js

Frisby is a REST API testing framework built on node.js and Jasmine.

Intsall Frsiby.js

Install Frisby.js with NPM locally. Execute the following command in your project folder.

sudo npm install frisby

Write Tests

Frisby tests start with frisby.create('Test Title'), where Test Title is a description for this test.

The tests are then followed by one of the HTTP methods (get, post, ..., etc). For example, .get(url) make a GET request to url.

The tests can be chained with expects after the HTTP methods. For example, .expectStatus(200) tests if the response status is 200.

The tests end with .toss() to generate the resulting Jasmine tests.

Many built-in matchers can be used to test HTTP status codes, JSON keys/values, JSON value types and others. Matchers can be found here.

Run Tests

Frisby.js uses the Jasmine-node test runner. Read more in the Jasmine-Node chapter.

Generate Test Reports

Test reports is generated by the Jasmine-Node test runner. Read more in the Jasmine-Node chapter.

A Simple Example

This simple example includes a HTTP server for GET, PUT, POST, DELETE requests. The server will return a json object with method field being the request type.

For example, the server will respond to a GET request with {"method":"GET"}.

Each test case includes sending a request and check the HTTP status code, response header, and response JSON body.

For example,

frisby.create('GET Method')
          .get('http://localhost:8080')
          .expectStatus(200)
          .expectHeaderContains('content-type', 'application/json')
          .expectJSON({
              method : 'GET'
          })
          .expectJSONTypes({
                method : String
          }).toss();

The example project can be found here.

To run the tests, execute the command node TestServer/app.js to start the server first.

After the server is running,

Http Server Listening on Port: 8080 ...

execute the command jasmine-node spec to run the tests. The following result should show.

4 tests, 16 assertions, 0 failures, 0 skipped

References

Frisby.js Website

Frisby.js Document